package com.example.petstore.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.service.CategoryService;
import com.example.petstore.service.ProductService;
import com.example.petstore.service.UserService;
import com.example.petstore.util.GiteeLogin;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SessionAttributes(value = {"giteeCustome", "giteeReason", "loginUser", "giteeId", "verifyCode", "tempPrice", "categoryList"})
@Controller
@CrossOrigin(origins = "*")
public class CommonController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @Autowired
    private DefaultKaptcha captchaProducer;

    @GetMapping("/")
    public String gotoo(Model model){
        CommonResponse commonResponse = categoryService.getCategoryList();
        model.addAttribute("categoryList", commonResponse.getData());
        return "index";
    }

    @RequestMapping("/index")
    public String index(Model model){
//        List<Product> showProductList;
        CommonResponse commonResponse = productService.getProductList();
        model.addAttribute("showProductList", commonResponse.getData());
        commonResponse = categoryService.getCategoryList();
        model.addAttribute("categoryList", commonResponse.getData());
//        System.out.println(commonResponse.getData());
//        System.out.println("controller: showProductList" + commonResponse.getData());

        return "catalog/main";
    }

    /**
     * 进入用户注册页面
     * @return
     */
    @GetMapping("register")
    public String register(){
        return "user/registerBuyer";
    }

    /**
     * 用户从gitee页面登录回来后进行的操作
     * @param code
     * @param model
     * @return
     */
    @GetMapping("/bindPhoneNumber")
    public String returnFromGitee(@RequestParam("code") String code, ModelMap model){
        String giteeCustome = (String) model.getAttribute("giteeCustome");
        String giteeReason = (String) model.getAttribute("giteeReason");
//        System.out.println(giteeCustome + " " + giteeReason);

        String userInfo = GiteeLogin.getUserInfo(code);
//        System.out.println(userInfo);
//        获取giteeId
        String giteeId = JSONObject.parseObject(userInfo).get("id").toString();
//        System.out.println(giteeId);

        if(giteeCustome.equals("user")){
            CommonResponse commonResponse = userService.findUserByGiteeId(giteeId);
            if(giteeReason.equals("login")){    // 如果是用户登录使用gitee
                if(commonResponse.isSuccess()){
                    model.addAttribute("loginUser", commonResponse.getData());
                    /* 到首页 */
                    return "catalog/main";
                }else{
                    model.addAttribute("errorMsg", commonResponse.getMessage());
                    /* 到登录 */
                    return "user/loginByName";
                }
            }

            if(giteeReason.equals("register")){ // 如果是用户使用gitee注册
                if(commonResponse.isSuccess()){
                    model.addAttribute("errorMsg", "该gitee账号已被他人绑定。");
                    /* 到登录 */
                    return "user/loginByName";
                }
                model.addAttribute("giteeId", giteeId);
//                todo:到bindphonumberandusername
                return "user/bind";
            }
        }


//        String userInfo = GiteeLogin.getUserInfo(code);
        // 取出用户名
//        String name = (String) JSONObject.parseObject(userInfo).get("name");
        // 取出头像
//        String avatar_url = (String) JSONObject.parseObject(userInfo).get("avatar_url");

        System.out.println(userInfo);
        return "test_index";
    }

    @GetMapping("/helpForm")
    public String helpForm(){
        return "help";
    }

    @GetMapping("/testzfb11")
    public String assa(){
        return "test_zfb";
    }

//    @GetMapping("/testsuccess")
//    @CrossOrigin(value = "http://127.0.0.1:5500") //指定具体ip允许跨域
//    @ResponseBody
//    private CommonResponse testsuccess(){
//        return CommonResponse.creatForSuccessMessage("success");
//    }
//    @GetMapping("/testerror")
//    @CrossOrigin(value = "http://127.0.0.1:5500") //指定具体ip允许跨域
//    @ResponseBody
//    private CommonResponse testerror(){
//        return CommonResponse.creatForError("error");
//    }

}
